Namespace Main

    Module mMain

        Public WithEvents frmLogin As fLogin
        Public WithEvents frmManager As fManager
        Public WithEvents frmEditor As fEditor
        Public WithEvents fAddSpawn As fAddMob

        Public sAppPath As String
        Public sServerPath As String

        Public iniConfig As Config.IniFile
        Public iniServer As Config.IniFile

        Public Sub Main()

            sAppPath = Strings.Left(Application.ExecutablePath, InStrRev(Application.ExecutablePath, "\"))

            iniConfig = New Config.IniFile(sAppPath & "config.ini")

            frmLogin = New fLogin
            Application.Run(frmLogin)

        End Sub

        Private Sub frmLogin_DoLogin() Handles frmLogin.DoLogin

            Dim tmpPath As String = iniConfig.GetString("DBLOGIN", "SERVERDIR", "")

            frmLogin.dlgFolder.Description = "Select SERVER ROOT directory:"
            If FileIO.FileSystem.DirectoryExists(tmpPath) Then
                frmLogin.dlgFolder.SelectedPath = tmpPath
            Else
                frmLogin.dlgFolder.SelectedPath = ""
            End If
            
            frmLogin.dlgFolder.ShowDialog()

            sServerPath = frmLogin.dlgFolder.SelectedPath

            Libs.sDataPath = iniConfig.GetString("DBLOGIN", "DATADIR", "")

            frmLogin.dlgFolder.Description = "Select CLIENT DATA directory:"
            If FileIO.FileSystem.DirectoryExists(Libs.sDataPath) Then
                frmLogin.dlgFolder.SelectedPath = Libs.sDataPath
            Else
                frmLogin.dlgFolder.SelectedPath = ""
            End If
            
            frmLogin.dlgFolder.ShowDialog()

            libs.sDataPath = frmLogin.dlgFolder.SelectedPath

            iniServer = New Config.IniFile(sServerPath & "\server.ini")

            iniConfig.WriteString("DBLOGIN", "SERVER", frmLogin.txtServer.Text)
            iniConfig.WriteString("DBLOGIN", "LOGIN", frmLogin.txtLogin.Text)
            iniConfig.WriteString("DBLOGIN", "PASSWORD", frmLogin.txtPassword.Text)
            iniConfig.WriteString("DBLOGIN", "SERVERDIR", sServerPath)
            iniConfig.WriteString("DBLOGIN", "DATADIR", Libs.sDataPath)

            iniServer.WriteString("DBLOGIN", "SERVER", frmLogin.txtServer.Text)
            iniServer.WriteString("DBLOGIN", "LOGIN", frmLogin.txtLogin.Text)
            iniServer.WriteString("DBLOGIN", "PASSWORD", frmLogin.txtPassword.Text)
            iniServer.WriteString("DBLOGIN", "CONNECTIONCOUNT", 8)

            Libs.LoadUILibs()

            With frmLogin
                If DatabaseManager.Connect(.txtServer.Text, .txtLogin.Text, .txtPassword.Text) Then

                    frmManager = New fManager
                    frmManager.Show()

                    .Hide()

                End If
            End With

        End Sub

        Private Sub frmManager_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles frmManager.FormClosing

            frmLogin.Close()

            libs.DisposeUILibs()

        End Sub
    End Module

End Namespace
